package com.yyq.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.yyq.result.R;
import com.yyq.servicebase.exceptionhandler.YyqException;
import com.yyq.vod.Utils.AliyunVodSDKUtils;
import com.yyq.vod.Utils.ConstantPropertiesUtil;
import com.yyq.vod.vodService.vodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yyq
 * @create 2021-04-21 0:45
 */
@CrossOrigin
@RestController
@RequestMapping("/eduvod/upfile")
public class vodController {
    @Autowired
    vodService vodService;

    //上传到阿里云视频
    @PostMapping("/upfile")
    public R upVideo(MultipartFile file){
        String videoId;
        videoId=vodService.upfile(file);
        return R.ok().data("videoId",videoId);
    }
    //删除阿里云云端视频 一个ｉｄ
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request=new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();
        }
        catch (Exception e){
            throw new YyqException(20001,"视频删除异常");
        }

    }

    //根据ｉｄ集合删除云端多个阿里云视频
    @DeleteMapping("/deleteMoreALiYunVideo")
    public R deleteMoreALiYunVideo(@RequestParam("VideoList") List<String> VideoList){
        vodService.deleteMoreVideo(VideoList);
        return R.ok();
    }

    //获取播放视频凭证
    @GetMapping("getPlayerAuth/{vid}")
    public R getPlayAuth(@PathVariable String vid){
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
        GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
        request.setVideoId(vid);
        try {
            GetVideoPlayAuthResponse acsResponse = client.getAcsResponse(request);
            String playAuth=acsResponse.getPlayAuth();
            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
            e.printStackTrace();
            return R.error();
        }

    }


}
